package cn.ctvonline.android.kaopu.widget.view;

import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Scroller;

/**
 * Created by dujian on 15/12/24.
 */
public class SliderListView extends ListView {
    private Context mContext;
    private int mSlidePosition;
    private View mSlideFillView;
    private View mSlideItemView;

    //外部控制可滑动条件
    private boolean canSlide = true;
    //内部控制可滑动条件
    private boolean slideAvailable = false;
    /**
     * 判断滚动的最小距离
     */
    private int mTouchSlop;

    private SlideDirection mSlideDirection;

    private int screenWidth;

    /**
     * 按下的位置
     */
    private int downX;
    private int downY;
    private Scroller scroller;

    public enum SlideDirection {
        LEFT, RIGHT
    }

    public SliderListView(Context context) {
        this(context, null);
    }

    public SliderListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SliderListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        mTouchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        windowManager.getDefaultDisplay().getSize(point);
        screenWidth = point.x;
        scroller = new Scroller(mContext, new LinearInterpolator());
    }

    /**
     * 分发事件，判断滑动条件，获取滑动viewItem
     *
     * @param event
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                downX = (int) event.getX();
                downY = (int) event.getY();
                mSlidePosition = pointToPosition(downX, downY);
                if (mSlidePosition == AdapterView.INVALID_POSITION) {
                    return super.dispatchTouchEvent(event);
                }
                //getChildAt 用于可视区域内
                mSlideItemView = getChildAt(mSlidePosition - getFirstVisiblePosition());
            }
            break;
            case MotionEvent.ACTION_MOVE: {
                int deltaX = (int) getX() - downX;
                int deltaY = (int) getY() - downY;
                if (Math.abs(deltaX) > Math.abs(deltaY) && Math.abs(deltaX) > mTouchSlop) {
                    slideAvailable = true;
                }
            }
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (slideAvailable && canSlide && mSlidePosition != AdapterView.INVALID_POSITION) {
            //阻止MotionEvent的向下传递
            requestDisallowInterceptTouchEvent(true);
            int action = event.getAction();
            int startX = (int) event.getX();
            switch (action) {
                case MotionEvent.ACTION_DOWN: {
                }
                break;
                case MotionEvent.ACTION_MOVE: {
                    int deltaX = (int) getX() - startX;
                    mSlideItemView.scrollBy(deltaX, 0);
                    return true;
                }
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP: {
//                    if(mSlideItemView.getScrollX() > -200){
//                        if(mSlideItemView.getScrollX() < -100 && mSlideItemView.getScrollX() > -200){
//                            scrollItemLeft();
//                        }else{
//                            scrollItemRight();
//                        }
//                    }
                    slideAvailable = false;
                }
                break;
            }
        }
        return super.onTouchEvent(event);
    }

    public void scrollItemLeft() {
        mSlideDirection = SlideDirection.LEFT;
        int deltaScrollX = (screenWidth - mSlideItemView.getScrollX());
        scroller.startScroll(mSlideItemView.getScrollX(), 0, deltaScrollX, Math.abs(deltaScrollX));
        postInvalidate();
    }

    public void scrollItemRight() {

    }
}
